<template>
  <!-- 新增弹窗 -->
  <el-dialog :title="title" :visible.sync="isShow" width="50%" @close="close">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="isShow=false">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRoleData, editRoleData } from '@/api/setting'

export default {
  data() {
    return {
      isShow: false,
      formData: {
        name: '',
        description: ''
      },
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '新增' : '修改'
    }
  },
  methods: {
    // 新增编辑
    async btnOk() {
      // 获取校验结果
      await this.$refs.form.validate()
      // 2、发送请求
      if (this.formData.id) {
        // 修改
        await editRoleData(this.formData)
      } else {
        // 添加
        await addRoleData(this.formData)
      }
      // 提示信息
      this.$message.success('操作成功')
      this.$emit('update')
      this.isShow = false
    },
    close() {
      // 重置表单
      this.formData = {}
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style>

</style>
